﻿/************************************************
*
* Copyright(c) ZeeGuo.com
*
* CLR : 4.0.30319.296
*
* FileName : Entity178ComicList.cs
*
* Author : Kurodo
*
* CreatTime : 2013/6/24 16:09:06
*           
*************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using ComicSpider.Model;
using Microsoft.JScript.Vsa;

namespace ComicSite
{
    public class Entity178ComicBook : EntityComicBook
    {
        protected override string GetBookName(string context)
        {
            return DataMatch.RegexsValue(context, "g_comic_name = (\\w{1,50});", 1);
            //Regex regName = new Regex("g_comic_name = \"\\w{1,50}\";");
            //return regName.Matches(context)[0].Value;
        }

        protected override string GetChapterName(string context)
        {
            return DataMatch.RegexsValue(context, "g_chapter_name = \"(\\w{1,50})\";", 1);
            //Regex regChapter = new Regex("g_chapter_name = \"(\\w{1,50})\";");
            //return regChapter.Matches(context)[0].Value;
        }

        protected override int GetPageCount(string context)
        {
            return int.Parse(DataMatch.RegexsValue(context, "g_max_pic_count = (\\d{1,3});", 1));
            //Regex regPage = new Regex("g_max_pic_count = \\d{1,3}\";");
            //return int.Parse(regPage.Matches(context)[0].Value);
        }

        public override List<string> GenerateComicPages(string context)
        {
            this.UrlPrefix = "http://imgfast.dmzj.com";
            string jsScript = DataMatch.RegexValue(context, "<script type=\"text/javascript\">([\\s\\S]*?)</script>", 0);
            jsScript = jsScript.Replace("\n", "");
            string jsEval = DataMatch.RegexValue(jsScript, "page = '';(eval\\(function\\(([\\s\\S]*?);)var g_comic_name", 1);

            VsaEngine ve = VsaEngine.CreateEngine();
            var eval = Microsoft.JScript.Eval.JScriptEvaluate(jsEval, ve);

            string rs = eval.ToString().Replace("\"", "");
            rs = HttpUtility.UrlDecode(rs);
            rs = rs.Substring(1, rs.Length - 1);
            rs = rs.Substring(0, rs.Length - 1);
            string[] rsArr = rs.Split(',');

            this.Comic_Pages = new List<string>();
            foreach (var r in rsArr)
            {
                this.Comic_Pages.Add(string.Format("{0}/{1}", this.UrlPrefix, r));
            }

            return this.Comic_Pages;
        }
    }
}
